package com.hm.ibsm.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.hm.common.def.DataStatusDef;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageParam;
import com.hm.common.util.CommonUtil;
import com.hm.ibsm.domain.MenuDomain;
import com.hm.ibsm.domain.QMenuDomain;
import com.hm.ibsm.manage.service.MenuService;
import com.hm.ibsm.manage.vo.menu.MenuVo;
import com.hm.ibsm.repositry.IMenuRepository;
import com.hm.ibsm.util.BaseDomain;
import com.hm.ibsm.util.BeanUtil;
import com.hm.ibsm.util.R.DB;
import com.querydsl.core.types.dsl.BooleanExpression;

/**
 * @author shishun.wang
 * @date 2017年2月10日 上午1:54:22
 * @version 1.0
 * @describe
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

	@Autowired
	private IMenuRepository menuRepository;

	@Override
	public void addMenu(MenuVo menu) {
		if (CommonUtil.isEmpty(menu.getMenuName())) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_NULL, "菜单名称不能为空");
		}
		MenuDomain domain = menuRepository.getMenuByMenuName(menu.getMenuName());
		if (CommonUtil.isNotEmpty(domain)) {
			throw ServiceException.warn("该菜单名称已经被使用");
		}

		MenuDomain menuDomain = new MenuDomain();
		BeanUtil.copyProperties(menu, menuDomain, BaseDomain.ignoreColumn());
		menuRepository.save(menuDomain);
	}

	@Override
	public void updateMenu(String menuId, MenuVo menu) {
		if (CommonUtil.isAnyEmpty(menuId, menu.getMenuName())) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_NULL, "菜单ID、菜单名称不能为空");
		}

		MenuDomain menuDomain = menuRepository.getMenuByMenuName(menu.getMenuName());
		if (CommonUtil.isNotEmpty(menuDomain) && menuId.equals(menuDomain.getId())) {
			throw ServiceException.warn("该菜单名称已经被使用");
		}

		MenuDomain domain = menuRepository.findOne(menuId);
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "菜单数据");
		}

		BeanUtil.copyProperties(menu, menuDomain, BaseDomain.ignoreColumn());
		menuRepository.save(menuDomain);
	}

	@Override
	public void deleteMenu(String menuId) {
		MenuDomain domain = menuRepository.findOne(menuId);
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "菜单数据");
		}
		domain.setStatus(DataStatusDef.DISABLE);
		menuRepository.save(domain);
	}

	@Override
	public MenuVo getMenuById(String menuId) {
		MenuDomain domain = menuRepository.findOne(menuId);
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "菜单数据");
		}

		MenuVo menuVo = new MenuVo();
		BeanUtil.copyProperties(domain, menuVo);
		return menuVo;
	}

	@Override
	public List<MenuVo> loadChildMenus(String parentId) {
		List<MenuVo> vos = new ArrayList<MenuVo>();
		QMenuDomain qMenuDomain = QMenuDomain.menuDomain;
		BooleanExpression pageable = qMenuDomain.status.eq(DataStatusDef.ENABLE);
		pageable = (CommonUtil.isEmpty(parentId)) ? pageable.and(qMenuDomain.parentId.isNull()) : pageable.and(qMenuDomain.parentId.eq(parentId));

		menuRepository.findAll(pageable, new Sort("level")).forEach(menu -> {
			MenuVo menuVo = new MenuVo();
			BeanUtil.copyProperties(menu, menuVo);
			vos.add(menuVo);
		});
		return vos;
	}

	@Override
	public PageInfo<MenuVo> query(PageParam page, String parentId) {
		PageRequest pageable = new PageRequest(page.getPage(), page.getSize(), new Sort(Direction.DESC, new String[] { DB.DEFAULT_SORT }));
		QMenuDomain qMenuDomain = QMenuDomain.menuDomain;
		Page<MenuDomain> pageContent = null;
		if (CommonUtil.isEmpty(parentId)) {
			pageContent = menuRepository
					.findAll(qMenuDomain.status.eq(DataStatusDef.ENABLE).and(qMenuDomain.parentId.isNull().or(qMenuDomain.parentId.isEmpty())), pageable);
		} else {
			pageContent = menuRepository.findAll(qMenuDomain.status.eq(DataStatusDef.ENABLE).and(qMenuDomain.parentId.eq(parentId)), pageable);
		}

		List<MenuVo> vos = new ArrayList<MenuVo>();
		pageContent.forEach(menu -> {
			MenuVo menuVo = new MenuVo();
			BeanUtil.copyProperties(menu, menuVo);
			vos.add(menuVo);
		});

		return new PageInfo<MenuVo>(vos, pageContent.getTotalPages(), pageContent.getTotalElements());
	}

}
